package com.example.backed.controller;

import com.example.backed.dto.GroupBuyCreateDTO;
import com.example.backed.dto.GroupBuyOrderCreateDTO;
import com.example.backed.dto.GroupBuyQueryDTO;
import com.example.backed.entity.GroupBuy;
import com.example.backed.entity.GroupBuyOrder;
import com.example.backed.service.GroupBuyService;
import com.example.backed.util.SecurityUtil;
import com.example.backed.vo.PageVO;
import com.example.backed.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "团购管理")
@RestController
@RequestMapping("/api/group-buys")
@RequiredArgsConstructor
public class GroupBuyController {

    private final GroupBuyService groupBuyService;

    @Operation(summary = "创建团购活动")
    @PostMapping
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    public Result<GroupBuy> createGroupBuy(@Validated @RequestBody GroupBuyCreateDTO createDTO) {
        GroupBuy groupBuy = groupBuyService.createGroupBuy(createDTO);
        return Result.success(groupBuy);
    }

    @Operation(summary = "取消团购活动")
    @PostMapping("/{id}/cancel")
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    public Result<Boolean> cancelGroupBuy(@Parameter(description = "团购ID") @PathVariable Long id) {
        boolean success = groupBuyService.cancelGroupBuy(id);
        return success ? Result.success(true) : Result.error("取消团购活动失败");
    }

    @Operation(summary = "获取团购活动详情")
    @GetMapping("/{id}")
    public Result<GroupBuy> getGroupBuyDetail(@Parameter(description = "团购ID") @PathVariable Long id) {
        GroupBuy groupBuy = groupBuyService.getGroupBuyDetail(id);
        return Result.success(groupBuy);
    }

    @Operation(summary = "查询团购活动列表")
    @GetMapping
    public Result<PageVO<GroupBuy>> getGroupBuyList(GroupBuyQueryDTO queryDTO) {
        PageVO<GroupBuy> pageVO = groupBuyService.getGroupBuyList(queryDTO);
        return Result.success(pageVO);
    }

    @Operation(summary = "创建团购订单")
    @PostMapping("/orders")
    public Result<GroupBuyOrder> createOrder(@Validated @RequestBody GroupBuyOrderCreateDTO createDTO) {
        Long userId = SecurityUtil.getCurrentUserId();
        GroupBuyOrder order = groupBuyService.createOrder(userId, createDTO);
        return Result.success(order);
    }

    @Operation(summary = "取消团购订单")
    @PostMapping("/orders/{id}/cancel")
    public Result<Boolean> cancelOrder(@Parameter(description = "订单ID") @PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        boolean success = groupBuyService.cancelOrder(userId, id);
        return success ? Result.success(true) : Result.error("取消订单失败");
    }

    @Operation(summary = "获取团购订单详情")
    @GetMapping("/orders/{id}")
    public Result<GroupBuyOrder> getOrderDetail(@Parameter(description = "订单ID") @PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        GroupBuyOrder order = groupBuyService.getOrderDetail(userId, id);
        return Result.success(order);
    }

    @Operation(summary = "获取用户的团购订单列表")
    @GetMapping("/orders/my")
    public Result<PageVO<GroupBuyOrder>> getUserOrders(
            @Parameter(description = "订单状态：0-待支付，1-已支付待成团，2-已成团，3-未成团，4-已取消") 
            @RequestParam(required = false) Integer status,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        Long userId = SecurityUtil.getCurrentUserId();
        PageVO<GroupBuyOrder> pageVO = groupBuyService.getUserOrders(userId, status, pageNum, pageSize);
        return Result.success(pageVO);
    }

    @Operation(summary = "支付团购订单")
    @PostMapping("/orders/{id}/pay")
    public Result<Boolean> payOrder(@Parameter(description = "订单ID") @PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        boolean success = groupBuyService.payOrder(userId, id);
        return success ? Result.success(true) : Result.error("支付失败");
    }
} 